-- Rain

if minetest.setting_getbool("lunoweather_rain")~= false then
	minetest.setting_set("lunoweather_rain", "true")
end

minetest.register_globalstep(function(dtime)
	if minetest.setting_getbool("lunoweather_snow")== true then
		local players = minetest.get_connected_players()
		for _, player in ipairs(players) do
			local ppos = player:getpos()
		
			local myWeather = lunoweather.get_weather(ppos)
			if myWeather=="rain" then
				local minp = lunoweather.addvectors(ppos, {x=-9, y=7, z=-9})
				local maxp = lunoweather.addvectors(ppos, {x= 9, y=7, z= 9})

				local luz = minetest.env:get_node_light(maxp, 0.5)
				if luz~=nil and luz >= 15 then
					local vel = {x=0, y=   -4, z=0}
					local acc = {x=0, y=-9.81, z=0}
					minetest.add_particlespawner({
						amount = math.ceil(25/#players),--math.ceil(arredonda para cima de 0.5), math.floor(arredonda para menor de 0.5)
						time = 0.5,
						minpos = minp,
						maxpos = maxp,
						minvel = vel,
						maxvel = vel,
						minacc = acc,
						maxacc = acc,
						minexptime = 0.8,
						maxexptime = 0.8,
						minsize = 25,
						maxsize = 25,
						collisiondetection = false,
						vertical = true, --Forca aparticula a fica sempre na vertical
						texture = "weather_rain.png",
						playername = player:get_player_name()
					})
				end
			end
		end
	end
end)

--[[
-- Enable this section if you have a very fast PC
minetest.register_abm({
	nodenames = {"group:crumbly", "group:snappy", "group:cracky", "group:choppy", "group:water"},
	neighbors = {"default:air"},
	interval = 10.0, 
	chance = 80,
	action = function (pos, node, active_object_count, active_object_count_wider)
		-- todo! chance must depend on rain value
		--if lunoweather.get_rain(pos) == 0 then return end
		local myWeather = lunoweather.get_weather(pos)
		if myWeather~=nil and myWeather~="rain" then return end
		
		if minetest.registered_nodes[node.name].drawtype ~= "normal"
			and minetest.registered_nodes[node.name].drawtype ~= "nodebox"
			and minetest.registered_nodes[node.name].drawtype ~= "flowingliquid"
			and minetest.registered_nodes[node.name].drawtype ~= "liquid"
			and minetest.registered_nodes[node.name].drawtype ~= "allfaces_optional" 
		then  
			return 
		end
		
		local np = lunoweather.addvectors(pos, {x=0, y=1, z=0})
		
		if minetest.env:get_node_light(np, 0.5) == 15 then
			if minetest.env:get_node(pos).name == "default:water_flowing" then
				minetest.env:add_node_level(pos, 1)
			elseif minetest.env:get_node(np).name == "air" then
				minetest.env:add_node(np, {name="water_flowing"})
				minetest.env:add_node_level(np)
			end
		end
	end
})


-- evaporate
minetest.register_abm({
	nodenames = {"default:water_flowing"},
	neighbors = {"default:air"},
	interval = 10.0, 
	chance = 10,
	action = function (pos, node, active_object_count, active_object_count_wider)
		-- todo! chance must depend on humidity
		--if lunoweather.get_rain(pos) > 0 or minetest.get_humidity(pos) > 90 then return end
		--if lunoweather.get_rain(pos) > 0 then return end
		local myWeather = lunoweather.get_weather(pos)
		if myWeather~=nil and myWeather~="rain" then return end
		
		
		local np = lunoweather.addvectors(pos, {x=0, y=1, z=0})
		--if minetest.env:get_node_light(np, 0.5) == 15 then
		if minetest.env:get_node(np).name == "air" then
			minetest.env:add_node_level(pos, -1)
		end
	end
})

--]]

